/*
 * s_common_loopClock.c
 *
 *  Created on: Sep 27, 2020
 *      Author: hemingway
 */
#include "s_common_loopClock.h"
#include <stdio.h>
#include <string.h>
#include "h_cc2640r2f_define.h"
#include "simple_peripheral_oad_offchip.h"

extern void s_start_clock_util(void);

typedef struct _s_common_loopClock_t
{
    unsigned int loopnum;
    unsigned int nowloopnum;
    unsigned int lastloopnum;
    s_common_loopClock_type clockType;
    void (* clockCallback)(s_common_loopClock_type clockType);
    struct _s_common_loopClock_t *next;
}s_common_loopClock_t;

static s_common_loopClock_t loopClocks_head;

s_common_state s_common_loopClock_init(void)
{
    loopClocks_head.next = NULL;
    return s_common_state_ok;
}

s_common_state s_common_loopClock_specialEvt_add(s_common_loopClock_type clockType, \
                                                 void *callBack, unsigned int loopnum)
{
    s_common_loopClock_t *tail_loopClock = &loopClocks_head;

    while(tail_loopClock->next != NULL) {
        if(clockType == tail_loopClock->next->clockType) {
            return s_common_existed;
        }
        tail_loopClock = tail_loopClock->next;
    }

    tail_loopClock->next = (s_common_loopClock_t *)H_CC2640R2F_DEBUG_MALLOC(sizeof(s_common_loopClock_t));
    if(NULL == tail_loopClock->next) {
        return s_common_malloc_failed;
    }


    tail_loopClock->next->loopnum = loopnum;
    tail_loopClock->next->nowloopnum = 0U;
    tail_loopClock->next->lastloopnum = 0U;
    tail_loopClock->next->clockType = clockType;
    tail_loopClock->next->clockCallback = callBack;
    tail_loopClock->next->next = NULL;


    return s_common_state_ok;
}

s_common_state s_common_loopClock_specialEvt_del(s_common_loopClock_type clockType)
{
    s_common_loopClock_t *this_last_loopClock = &loopClocks_head;
    s_common_loopClock_t *this_loopClock;

    while(this_last_loopClock->next != NULL) {
        if(clockType == this_last_loopClock->next->clockType) {
            break;
        }
        this_last_loopClock = this_last_loopClock->next;
    }

    if(NULL == this_last_loopClock->next) {
        return s_common_notExisted;
    }

    this_loopClock = this_last_loopClock->next;
    this_last_loopClock->next = this_loopClock->next;

    this_loopClock->clockCallback = NULL;
    this_loopClock->next = NULL;
    H_CC2640R2F_DEBUG_FREE(this_loopClock);
    this_loopClock = NULL;

    return s_common_state_ok;
}

s_common_state s_common_loopClock_specialEvt_start(s_common_loopClock_type clockType)
{
    s_common_loopClock_t *this_loopClock = loopClocks_head.next;

    while(this_loopClock != NULL) {
        if(clockType == this_loopClock->clockType) {
            break;
        }
        this_loopClock = this_loopClock->next;
    }

    if(NULL == this_loopClock) {
        return s_common_notExisted;
    }

    this_loopClock->nowloopnum = this_loopClock->loopnum;
    this_loopClock->lastloopnum = 0U;

    s_start_clock_util();

    return s_common_state_ok;
}

s_common_state s_common_loopClock_specialEvt_stop(s_common_loopClock_type clockType)
{
    s_common_loopClock_t *this_loopClock = loopClocks_head.next;

    while(this_loopClock != NULL) {
        if(clockType == this_loopClock->clockType) {
            break;
        }
        this_loopClock = this_loopClock->next;
    }

    if(NULL == this_loopClock) {
        return s_common_notExisted;
    }

    this_loopClock->nowloopnum = 0U;
    this_loopClock->lastloopnum = 0U;

    return s_common_state_ok;
}

s_common_state s_common_loopClock_specialEvt_edit_time(s_common_loopClock_type clockType, unsigned int loopnum)
{
    s_common_loopClock_t *this_loopClock = loopClocks_head.next;

    while(this_loopClock != NULL) {
        if(clockType == this_loopClock->clockType) {
            break;
        }
        this_loopClock = this_loopClock->next;
    }

    if(NULL == this_loopClock) {
        return s_common_notExisted;
    }

    this_loopClock->loopnum = loopnum;

    return s_common_state_ok;
}

void s_common_loopClock_handle(void)
{
    unsigned char clock_ctn_flag = 0U;

//    printf("s_common_loopClock_handle\r\n");

    s_common_loopClock_t *this_loopClock = loopClocks_head.next;

    while(this_loopClock != NULL) {

        if(this_loopClock->nowloopnum > 0U) {
            this_loopClock->lastloopnum = this_loopClock->loopnum;
            this_loopClock->nowloopnum--;
            clock_ctn_flag = 1U;
        } else {
            if(this_loopClock->lastloopnum > 0U)
            {
                this_loopClock->lastloopnum = 0U;
                this_loopClock->clockCallback(this_loopClock->clockType);
            }
        }
        this_loopClock = this_loopClock->next;
    }

    if(clock_ctn_flag)
    {
        s_start_clock_util();
    }
}

//typedef struct _s_common_loopClock_t
//{
//    unsigned int loopnum;
//    unsigned int nowloopnum;
//    unsigned int lastloopnum;
//    s_common_loopClock_type clockType;
//    void (* clockCallback)(s_common_loopClock_type clockType);
//    struct _s_common_loopClock_t *next;
//}s_common_loopClock_t;
//
//static s_common_loopClock_t loopClocks_head;
//
//s_common_state s_common_loopClock_init(void)
//{
//    loopClocks_head.next = NULL;
//    return s_common_state_ok;
//}
//
//s_common_state s_common_loopClock_specialEvt_add(s_common_loopClock_type clockType, \
//                                                 void *callBack, unsigned int loopnum)
//{
//    s_common_loopClock_t *tail_loopClock = &loopClocks_head;
//
//    while(tail_loopClock->next != NULL) {
//        if(clockType == tail_loopClock->next->clockType) {
//            return s_common_existed;
//        }
//        tail_loopClock = tail_loopClock->next;
//    }
//
//    tail_loopClock->next = (s_common_loopClock_t *)H_CC2640R2F_DEBUG_MALLOC(sizeof(s_common_loopClock_t));
//    if(NULL == tail_loopClock->next) {
//        return s_common_malloc_failed;
//    }
//
//
//    tail_loopClock->next->loopnum = loopnum;
//    tail_loopClock->next->nowloopnum = 0U;
//    tail_loopClock->next->lastloopnum = 0U;
//    tail_loopClock->next->clockType = clockType;
//    tail_loopClock->next->clockCallback = callBack;
//    tail_loopClock->next->next = NULL;
//
//
//    return s_common_state_ok;
//}
//
//s_common_state s_common_loopClock_specialEvt_del(s_common_loopClock_type clockType)
//{
//    s_common_loopClock_t *this_last_loopClock = &loopClocks_head;
//    s_common_loopClock_t *this_loopClock;
//
//    while(this_last_loopClock->next != NULL) {
//        if(clockType == this_last_loopClock->next->clockType) {
//            break;
//        }
//        this_last_loopClock = this_last_loopClock->next;
//    }
//
//    if(NULL == this_last_loopClock->next) {
//        return s_common_notExisted;
//    }
//
//    this_loopClock = this_last_loopClock->next;
//    this_last_loopClock->next = this_loopClock->next;
//
//    this_loopClock->clockCallback = NULL;
//    this_loopClock->next = NULL;
//    H_CC2640R2F_DEBUG_FREE(this_loopClock);
//    this_loopClock = NULL;
//
//    return s_common_state_ok;
//}
//
//s_common_state s_common_loopClock_specialEvt_start(s_common_loopClock_type clockType)
//{
//    s_common_loopClock_t *this_loopClock = loopClocks_head.next;
//
//    while(this_loopClock != NULL) {
//        if(clockType == this_loopClock->clockType) {
//            break;
//        }
//        this_loopClock = this_loopClock->next;
//    }
//
//    if(NULL == this_loopClock) {
//        return s_common_notExisted;
//    }
//
//    this_loopClock->nowloopnum = this_loopClock->loopnum;
//    this_loopClock->lastloopnum = 0U;
//
//    s_start_clock_util();
//
//    return s_common_state_ok;
//}
//
//s_common_state s_common_loopClock_specialEvt_stop(s_common_loopClock_type clockType)
//{
//    s_common_loopClock_t *this_loopClock = loopClocks_head.next;
//
//    while(this_loopClock != NULL) {
//        if(clockType == this_loopClock->clockType) {
//            break;
//        }
//        this_loopClock = this_loopClock->next;
//    }
//
//    if(NULL == this_loopClock) {
//        return s_common_notExisted;
//    }
//
//    this_loopClock->nowloopnum = 0U;
//    this_loopClock->lastloopnum = 0U;
//
//    return s_common_state_ok;
//}
//
//void s_common_loopClock_handle(void)
//{
//    unsigned char clock_ctn_flag = 0U;
//
//    s_common_loopClock_t *this_loopClock = loopClocks_head.next;
//
//    while(this_loopClock != NULL) {
//
//        if(this_loopClock->nowloopnum > 0U) {
//            this_loopClock->lastloopnum = this_loopClock->loopnum;
//            this_loopClock->nowloopnum--;
//            clock_ctn_flag = 1U;
//        } else {
//            if(this_loopClock->lastloopnum > 0U)
//            {
//                this_loopClock->lastloopnum = 0U;
//                this_loopClock->clockCallback(this_loopClock->clockType);
//            }
//        }
//        this_loopClock = this_loopClock->next;
//    }
//
//    if(clock_ctn_flag)
//    {
//        s_start_clock_util();
//    }
//}


